// pages/user/recharge/recharge.js
var http = require('../../../utils/http.js');
Page({
  /**
   * 当选择场所改变时，改变充值套餐
   */
  bindPlaceChange: function (e) {
    let index = parseInt(e.detail.value)
    this.setData({
      "rechargeForm.place_id": this.data.placeList[index].id
    });
    this.setData({
      "rechargeForm.place_name": this.data.placeList[index].place_name
    })
    this.getPackageList();
  },
  /***
   * 监听子组件触发添加减少按钮
   */
  changeCount(e){
    let that = this;
    this.setData({
      "rechargeForm.pay_num": e.detail.count
    });
    this.setData({
      "rechargeForm.pay_total_price": parseFloat(that.data.rechargeForm.pay_price * that.data.rechargeForm.pay_num).toFixed(2)
    });
  },
  /**
   * 当选择套餐改变时修改价格
   */
  bindPackageChange: function (e) {
    let index = parseInt(e.detail.value)
    this.setData({
      "rechargeForm.package_id": this.data.packageList[index].package_id
    });
    this.setData({
      "rechargeForm.package_name": this.data.packageList[index].package_name
    });
    this.setData({
      "rechargeForm.pay_price": this.data.packageList[index].package_price
    });
    this.setData({
      "rechargeForm.package_companyType": this.data.packageList[index].package_companyType
    });
    this.setData({
      "rechargeForm.pay_total_price": parseFloat(this.data.packageList[index].package_price * this.data.rechargeForm.pay_num).toFixed(2)
    });
    this.getPackageList();
  },
  /**
   * 当点击充值按钮时
   */
  recharge: function () {
    let that = this;
    if (!this.data.rechargeForm.place_id){
      wx.showToast({
        title: '请选择场所',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.rechargeForm.package_id) {
      wx.showToast({
        title: '请选择套餐',
        icon: 'none'
      })
      return false;
    }
    http.POST({
      url: 'api/order/recharge',
      data:that.data.rechargeForm,
      success: function(res){
        if(res.data.error===0){
          wx.showToast({
            title: '充值成功',
          })
          setTimeout(function(){
            wx.navigateTo({
              url: '../index/index',
            })
          },1500)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  //获取运营者的用户场所信息
  getPlaceList(){
    let that = this;
    http.GET({
      url: 'api/place/placePublicList',
      success: function(res){
        that.setData({
          "placeList": res.data.data
        })
      }
    })
  },
  //获取一个场所下套餐列表0
  getPackageList(){
    let data = {
      place_id: this.data.rechargeForm.place_id
    }
    let that = this;
    http.GET({
      url: 'api/package/packagePlaceList',
      data: data,
      success: function (res) {
        that.setData({
          "packageList": res.data.data
        })
      }
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    rechargeForm: {
      place_name: '请选择充值场所',
      place_id: '',
      package_name: '请选择充值类型',
      pay_price: 0,//单价
      package_companyType: '',//套餐类型
      pay_num:1,//数量
      pay_total_price: 0,
      package_id: '',
      user_id: '',
      device_id: '',
    },
    index: 0,
    placeList: [],
    packageList: []
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let item = wx.getStorage({
      key: 'item',
      success: function (res) {
        let item = JSON.parse(res.data);
        that.setData({
          'rechargeForm.user_id': item.id
        })
        that.setData({
          'rechargeForm.device_id': item.device_id
        })
      },
    })
    this.getPlaceList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})